/*
 * main implementation: use this 'C' sample to create your own application
 *
 */

#include <stdio.h>

#include "derivative.h" /* include peripheral declarations */

int main(void) {
	
	unsigned long int status;
	
	printf("Enabling Port A clocks\n");
	SIM_SCGC5 = SIM_SCGC5 | (1 << 9) | (1 << 13);
	
	/* Setting pin settings for LED input. */
	PORTA_PCR10 = PORTA_PCR10 | (1 << 8);
	PORTA_PCR29 = PORTA_PCR29 | (1 << 8);
	PORTA_PCR11 = PORTA_PCR11 | (1 << 8);
	PORTA_PCR28 = PORTA_PCR28 | (1 << 8);
	
	/* Settings for switch pins. */
	PORTA_PCR19 = (1 << 8) | (1 << 1) | 1;
	PORTE_PCR26 = (1 << 8) | (1 << 1) | 1;
	
	printf("Setting GPIOA pins.\n");
	GPIOA_PDDR = 1<<10 | 1<<11 | 1<<28 | 1<<29;
	GPIOE_PDDR = 0;
	
	GPIOA_PSOR = 0xdffffbff;
	
	/* Now start reading the push pins. */
	for(;;) {
		
	   	if ((GPIOA_PDIR & (1 << 19)) == 0){
	   		printf("Toggling LED.\n");
	   		GPIOA_PTOR = 1 << 11;
	   	}
	   	
	   	/* Get current state of the GPIO port. */
	   	status = GPIOA_PDOR;
	   	printf("GPIOE_PDIR: 0x%08x\n", GPIOE_PDIR);
	   	
	   	if ((GPIOE_PDIR & (1 << 26)) == 0){
	   		printf("Setting LED on.\n");
	   		GPIOA_PDOR = status & ~(1 << 28);
	   	} else {
			printf("Setting LED off.\n");
	   		GPIOA_PDOR = status | (1 << 28);
		}
	   	
	}
	
	return 0;
}
